import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import '../services/native_video_player_service.dart';

class NativeVideoPlayer extends StatelessWidget {
  final File videoFile;

  const NativeVideoPlayer({
    super.key,
    required this.videoFile,
  });

  Future<void> _playVideo(BuildContext context) async {
    try {
      print('使用原生播放器播放: ${videoFile.path}');
      
      final windowId = await NativeVideoPlayerService.playVideo(videoFile.path);
      
      if (windowId != null) {
        print('视频播放器已打开，窗口ID: $windowId');
        
        // 显示成功消息
        if (context.mounted) {
          ScaffoldMessenger.of(context).showSnackBar(
            SnackBar(
              content: Text('正在播放: ${videoFile.path.split('/').last}'),
              duration: const Duration(seconds: 2),
            ),
          );
        }
      }
    } on PlatformException catch (e) {
      print('播放失败: ${e.message}');
      
      if (context.mounted) {
        String errorMessage = '视频播放失败';
        
        switch (e.code) {
          case 'FILE_NOT_FOUND':
            errorMessage = '视频文件不存在';
            break;
          case 'UNSUPPORTED_PLATFORM':
            errorMessage = '不支持的平台';
            break;
          default:
            errorMessage = e.message ?? '未知错误';
        }
        
        ScaffoldMessenger.of(context).showSnackBar(
          SnackBar(
            content: Text(errorMessage),
            backgroundColor: Colors.red,
          ),
        );
      }
    } catch (e) {
      print('播放失败: $e');
      
      if (context.mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(
            content: Text('视频播放失败'),
            backgroundColor: Colors.red,
          ),
        );
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: Colors.black,
      child: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            // 播放图标
            GestureDetector(
              onTap: () => _playVideo(context),
              child: Container(
                width: 120,
                height: 120,
                decoration: BoxDecoration(
                  color: Colors.white.withOpacity(0.9),
                  shape: BoxShape.circle,
                  boxShadow: [
                    BoxShadow(
                      color: Colors.black.withOpacity(0.3),
                      blurRadius: 10,
                      spreadRadius: 2,
                    ),
                  ],
                ),
                child: const Icon(
                  Icons.play_arrow,
                  size: 60,
                  color: Colors.black87,
                ),
              ),
            ),
            
            const SizedBox(height: 24),
            
            // 文件信息
            Container(
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
              decoration: BoxDecoration(
                color: Colors.black.withOpacity(0.7),
                borderRadius: BorderRadius.circular(8),
              ),
              child: Column(
                children: [
                  Text(
                    videoFile.path.split('/').last,
                    style: const TextStyle(
                      color: Colors.white,
                      fontSize: 16,
                      fontWeight: FontWeight.w500,
                    ),
                    textAlign: TextAlign.center,
                  ),
                  const SizedBox(height: 4),
                  FutureBuilder<int>(
                    future: videoFile.length(),
                    builder: (context, snapshot) {
                      if (snapshot.hasData) {
                        final sizeInMB = (snapshot.data! / 1024 / 1024).toStringAsFixed(1);
                        return Text(
                          '$sizeInMB MB • 点击播放',
                          style: const TextStyle(
                            color: Colors.white70,
                            fontSize: 14,
                          ),
                        );
                      }
                      return const Text(
                        '点击播放',
                        style: TextStyle(
                          color: Colors.white70,
                          fontSize: 14,
                        ),
                      );
                    },
                  ),
                ],
              ),
            ),
            
            const SizedBox(height: 16),
            
            // 说明文字
            const Text(
              '使用原生播放器在新窗口中播放视频',
              style: TextStyle(
                color: Colors.white54,
                fontSize: 12,
              ),
            ),
          ],
        ),
      ),
    );
  }
}